/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Frame_2_SelectSubjectFrame.java]
 * Creation Date        [12-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_04_other.examApp.ui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import pkg_04_other.examApp.dao.ExaminationFactory;

/**
 * 科目选择面板
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Frame_2_SelectSubjectFrame extends JFrame implements
		ActionListener {

	private static final long serialVersionUID = 2800492506163472000L;

	/** 按钮, 下拉列表, 标签组件 */
	private final JButton bt_begin = new JButton("开始考试");
	private final JButton bt_exit = new JButton("退出");
	private final JComboBox<String> subjects = new JComboBox<String>();
	private final JLabel welcome_label = new JLabel();
	private final JLabel subject_label = new JLabel();

	private void initJComboBox() {
		// 获得所有科目
		List<String> allSubject = ExaminationFactory.getExaminationInstance(
				"paper").getSubjects();
		// 初始化科目下拉列表
		for (String subject : allSubject) {
			subjects.addItem(subject);
		}
	}

	private void init() {
		initJComboBox();
		setLayout(new GridLayout(3, 1));
		String studentName = ExaminationFactory.getExaminationInstance(
				"student").getName();

		welcome_label.setText(studentName + " 欢迎进入考试系统, 祝您考出好成绩");
		this.add(welcome_label);

		subject_label.setText("请选择考试科目:");
		JPanel panel1 = new JPanel();
		panel1.add(subject_label);
		panel1.add(subjects);
		this.add(panel1);

		JPanel panel2 = new JPanel();
		panel2.add(bt_begin);
		panel2.add(bt_exit);
		this.add(panel2);

		bt_begin.addActionListener(this);
		bt_exit.addActionListener(this);
	}

	public Frame_2_SelectSubjectFrame() {
		super("选择考试科目");
		init();
		this.setLocation(Frame_1_LoginFrame.WIDTH / 2,
				Frame_1_LoginFrame.HEIGHT / 2);
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		String subject = (String) subjects.getSelectedItem();
		String message = "您选择的是:" + subject + " 科目吗? ";
		if ("开始考试".equals(command)) {
			int result = JOptionPane.showConfirmDialog(this, message);
			if (result == JOptionPane.YES_OPTION) {
				ExaminationFactory.getExaminationInstance("paper").setSubject(
						subject);
				new Frame_3_ExamMainFrame();
				setVisible(false);
			} else {
				JOptionPane.showMessageDialog(this, "重新选择考试科目");
			}
		}
		if ("退出".equals(command)) {
			System.exit(0);
		}
	}
}